package org.example.server.sever.serviceImpl;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.common.result.Result;
import org.example.pojo.user.Emp;

import org.example.pojo.user.userLogin;
import org.example.pojo.user.userRegistDto;
import org.example.server.mapper.userMapper;
import org.example.server.sever.userService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class userServiceImpl extends ServiceImpl<userMapper, Emp> implements userService {

    private final RedisTemplate redisTemplate;


    public Result<Object>  existsByIdAndRegist(userRegistDto Dto) {

//        boolean exists = lambdaQuery()
//                .eq(Emp::getName, Dto.getName())
//                .exists();
        boolean exists1 = lambdaQuery()
                .eq(Emp::getYouxiang, Dto.getYouxiang())
                .exists();
        if(exists1){
            return Result.error("邮箱已被注册，此次注册失败");
        }
//        if(exists){
//            return Result.success("用户名已被注册，此次注册失败");
//        }
        String code=Dto.getCode();
        if(!exists1 && code.equals(redisTemplate.opsForValue().get(Dto.getYouxiang())) ){
            Emp emp=new Emp();
            BeanUtils.copyProperties(Dto, emp);
            emp.setName("user"+Dto.getYouxiang());
            boolean save = save(emp);
            if(save){
                return Result.success("regist success");
            }

        }

        return Result.error("regist failed");

    }

    @Override
    public Integer login(userLogin login) {
        Emp emp = lambdaQuery()
                .eq(Emp::getYouxiang, login.getYouxiang())
                .eq(Emp::getPassword, login.getPassword())
                .select(Emp::getId)
                .one();
        return emp.getId();


    }

}
